//
//  StatusReccomentView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/21.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

class StatusReccomentView: UIView {
    
    /// 视图模型
    var viewModel: StatusViewModel?{
        
        didSet{
            //转发文本
            
            reweetedLabel.text = viewModel?.retWeedtedText
            
            //判断是否有转发 微博
            if viewModel?.status.retweeted_status == nil {
                
                reweetedLabel.snp_updateConstraints(closure: { (make) -> Void in
                    make.top.equalTo(self)
                })
                
                pictureView.snp_updateConstraints(closure: { (make) -> Void in
                    make.size.equalTo(CGSizeZero)
                    make.bottom.equalTo(self)
                    make.top.equalTo(reweetedLabel.snp_bottom)
                })
                
                return
            }
            
            
            
            //更新配图视图大小
            let offset = viewModel?.status.retweeted_status?.pic_urls?.count > 0 ? StatusCellLayout.margin : 0
            
            reweetedLabel.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(self).offset(StatusCellLayout
                .margin)
            }
            
            pictureView.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(reweetedLabel.snp_bottom).offset(offset)
                
                make.size.equalTo(viewModel!.retweetedPictureViewSize)
                
                make.bottom.equalTo(self).offset(-StatusCellLayout.margin)
            }
            //设置图像
            pictureView.pic_urls = viewModel?.status.retweeted_status?.pic_urls
            
            //设置图像大小
            pictureView.updataImageSize(viewModel!.retweetedPictureViewSize)
            
            
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有控件
    /// 被转发文字
    private let reweetedLabel:UILabel = UILabel(text: "@作者:微博",  fontsize: 14)
    /// 配图视图
    private let pictureView:StatusPictureView = StatusPictureView()
    
    
}
// MARK: - 设置界面
extension StatusReccomentView{
    
    private func setupUI(){
        
        backgroundColor = GXLcellColor
        
        //添加控件
        addSubview(reweetedLabel)
        addSubview(pictureView)
        
        let layout = StatusCellLayout
        //自动布局
        reweetedLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self).offset(layout.margin)
            make.right.equalTo(self).offset(-layout.margin)
            make.top.equalTo(self).offset(layout.margin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(reweetedLabel)
            make.top.equalTo(reweetedLabel.snp_bottom).offset(layout.margin)
            
            make.size.equalTo(CGSizeZero)
            
            make.bottom.equalTo(self).offset(-layout.margin)
            
            
        }
    }

}
